package com.test.daily.leetcode.y2022.m06.day0624.home.v03;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * @author Tom on 2022/6/24
 */
public class Solution {
    public static class TreeNode{
        int value;
        TreeNode left;
        TreeNode right;
        public TreeNode(int value){
            this.value = value;
        }
    }
    public static List<Integer> maxValueEveryLevel(TreeNode root){
        if(root == null){
            return new ArrayList<Integer>();
        }
        List<Integer> ans = new ArrayList<>();
        Queue<TreeNode> queue = new ArrayDeque();
        queue.add(root);
        while(!queue.isEmpty()){
            int size= queue.size();
            int maxValue = Integer.MIN_VALUE;
            while(size > 0){
                size--;
                TreeNode cur = queue.poll();
                maxValue = Math.max(cur.value, maxValue);
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
            }
            ans.add(maxValue);
        }
        return ans;
    }
}
